/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.cei.rmi.observer.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import test.cei.rmi.observer.common.dto.MensajeDTO;
import test.cei.rmi.observer.common.dto.Persona;

/**
 *
 * @author Francisco
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2databasePU");
        EntityManager em = factory.createEntityManager();

        Persona p = new Persona();
        p.setNombre("pepeN");
        p.setApellido("pepeA");

        em.getTransaction().begin();
        em.persist(p);
        p.setNombre("h");
        em.getTransaction().commit();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            Servidor s = new Servidor();
//            Servidor stub = (Servidor) UnicastRemoteObject.exportObject(s, 0);
            Registry r = LocateRegistry.createRegistry(9999);
            r.rebind(Servidor.REBIND_NAME, s);
            String c;
            String d;
            System.out.println("Servidor en linea");
            try {
                c = br.readLine();
                do {
                    d = br.readLine();
                    c = br.readLine();
                    s.procesaMensaje(new MensajeDTO(d, c));
                } while (!c.equals("STOP"));
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
